using System;
using EPiServer.Common;
using EPiServer.Common.Attributes;
using EPiServer.Common.Attributes.DataTypes;

namespace EPiAbstractions.Common.Attributes
{
    public class AttributeHandlerFacade : IAttributeHandlerFacade
    {
        private static AttributeHandlerFacade _instance;

        public AttributeHandlerFacade()
        {
            AttributeHandler.AttributeAdded += OnAttributeAdded;
            AttributeHandler.AttributeUpdated += OnAttributeUpdated;
            AttributeHandler.AttributeRemoved += OnAttributeRemoved;
        }

        public static AttributeHandlerFacade Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new AttributeHandlerFacade();

                return _instance;
            }

            set { _instance = value; }
        }

        #region IAttributeHandlerFacade Members

        public event EPiServerCommonEventHandler AttributeAdded;
        public event EPiServerCommonEventHandler AttributeUpdated;
        public event EPiServerCommonEventHandler AttributeRemoved;

        public virtual IAttribute GetAttribute(Int32 id)
        {
            return AttributeHandler.GetAttribute(id);
        }

        public virtual AttributeCollection GetAttributes(Type type)
        {
            return AttributeHandler.GetAttributes(type);
        }

        public virtual AttributeCollection GetAttributes(IAttributeExtendableEntity entity)
        {
            return AttributeHandler.GetAttributes(entity);
        }

        public virtual AttributeCollection GetAttributes(IAttributeExtendableEntity entity, ISite site)
        {
            return AttributeHandler.GetAttributes(entity, site);
        }

        public virtual AttributeValueChoiceCollection GetAttributeValueChoices(IAttribute attribute)
        {
            return AttributeHandler.GetAttributeValueChoices(attribute);
        }

        public virtual IAttribute AddAttribute(IAttribute attrib)
        {
            return AttributeHandler.AddAttribute(attrib);
        }

        public virtual void UpdateAttribute(IAttribute attrib)
        {
            AttributeHandler.UpdateAttribute(attrib);
        }

        public virtual void RemoveAttribute(IAttribute attrib)
        {
            AttributeHandler.RemoveAttribute(attrib);
        }

        public virtual IAttributeDataTypeProvider GetDataTypeProvider(Type complexType)
        {
            return AttributeHandler.GetDataTypeProvider(complexType);
        }

        #endregion

        public virtual void OnAttributeAdded(String sender, EPiServerCommonEventArgs e)
        {
            if (AttributeAdded != null)
                AttributeAdded(sender, e);
        }

        public virtual void OnAttributeUpdated(String sender, EPiServerCommonEventArgs e)
        {
            if (AttributeUpdated != null)
                AttributeUpdated(sender, e);
        }

        public virtual void OnAttributeRemoved(String sender, EPiServerCommonEventArgs e)
        {
            if (AttributeRemoved != null)
                AttributeRemoved(sender, e);
        }
    }
}